package agile.sudoku.core;
import static org.junit.Assert.*;

import org.junit.Test;



/**
 * Unit Test for initialized Sudoku Grid.
 *
 * @author max
 *
 */
public class _SudokuGrid {

	@Test
	public void getInitializedSudokuGridSize() {
		SudokuGrid sgrid = setupBasic9bz9Grid();
		assertEquals("Grid size",81,sgrid.numberOfCells());
	}

	
	
	@Test
	public void initializeCell() {
		Cell c = new Cell(3);
		assertEquals("initialized Cell values",3,c.getValue());
	}
	
	@Test
	public void startStateGeneratorTest() {
		StartStateGenerator generator = new StartStateGenerator();
		assertNotNull("Generator elements not Null",generator.getElements());
		assertTrue("Elements generated greater 0", 0 < generator.getElements().size());
	}
	
	@Test
	public void notEmptyCellValues() {
		// setup
		// execution
		// expectations
		
		SudokuGrid sgrid = setupBasic9bz9Grid();
		System.out.println(sgrid.toString());
		expectAllCellsAreSet(sgrid);
	}



	private void expectAllCellsAreSet(SudokuGrid sgrid) {
		for (int i = 0; i < sgrid.getGridSize(); i++) {
			for (int j = 0; j < sgrid.getGridSize(); j++) {
				if (sgrid.getCell(i, j).isEmpty()) {
					assertEquals("Init Grid: Empty Cell has value = 0",0,sgrid.getCell(i, j).getValue());
					assertTrue("Init Grid: Empty Cell is enabled",sgrid.getCell(i, j).isEnabled());
				} else {
					assertTrue("Init Grid: Not Empty Cell has value bigger than 0",0 < sgrid.getCell(i, j).getValue());
					assertFalse("Init Grid: Not Empty Cell is disabled", sgrid.getCell(i, j).isEnabled());
				}
			}
		}
	}



	private SudokuGrid setupBasic9bz9Grid() {
		SudokuGrid sgrid = new SudokuGrid(3);
		return sgrid;
	}
	
	@Test
	public void doNotSetDisabledCells() {
		SudokuGrid sgrid = setupBasic9bz9Grid();
		assertFalse("Already setted cell can not be set", sgrid.setCell(0,0,7));
	}
	
	@Test
	public void setEnabledCellnoConflict() {
		SudokuGrid sgrid = setupBasic9bz9Grid();
		assertTrue("Set Cell where no conflict occours", sgrid.setCell(0,2,4));
		assertEquals("Previously set value is in the cell",4,sgrid.getCell(0, 2).getValue());
	}
	
	@Test
	public void setEnabledCellWithConflict() {
		SudokuGrid sgrid = setupBasic9bz9Grid();
		assertFalse("Conflict in Grid Row",sgrid.setCell(0,2,7));
		assertFalse("Conflict in Grid Col",sgrid.setCell(1,1,6));
		assertFalse("Conflict in Tile",sgrid.setCell(1,1,5));
		assertFalse("Conflict in Tile",sgrid.setCell(1,1,8));
	}
	
	@Test
	public void setEnabledCellWithoutConflict() {
		SudokuGrid sgrid = setupBasic9bz9Grid();
		assertTrue("Set cell without conflict",sgrid.setCell(2, 6, 5));
		assertEquals("Value should be set to 5",5,sgrid.getCell(2, 6).getValue());
		assertTrue("Set cell without conflict",sgrid.setCell(0, 6, 9));
		assertEquals("Value should be set to 9",9,sgrid.getCell(0, 6).getValue());
	}
	
	@Test
	public void negativeTests() {
		SudokuGrid sgrid = setupBasic9bz9Grid();
		assertFalse("Set something out of grid",sgrid.setCell(10, 10, 8));
		assertFalse("Value to high",sgrid.setCell(1,1,12));
		assertFalse("Value to low",sgrid.setCell(1,1,0));

	}
}
